﻿Public Class Frm_Manajemen_Anggota_edit

    Dim changePicture As Boolean = False

    Public Sub initDataEdit(ByVal username As String)
        ' get data from database
        Db.FlushCache()
        Db.Selects("*")
        Db.From("tbl_anggota")
        Db.Where("nim", username)
        Connection.ExecuteToDataReader(Db.GetQueryString)
        If Connection.Read.HasRows Then
            With Connection.Read
                .Read()
                txt_NIM.Text = .Item("nim").ToString
                txt_Nama.Text = .Item("nama").ToString
                txt_Tmp_Lahir.Text = .Item("tmp_lahir").ToString
                cmb_tanggal_lahir.Text = .Item("tgl_lahir").ToString
                txt_Alamat.Text = .Item("alamat").ToString
                txt_Telp.Text = .Item("telp").ToString
                txt_Email.Text = .Item("email").ToString
                txt_Jurusan.Text = .Item("jurusan").ToString
                cmb_Tahun.Text = .Item("tgl_lahir").ToString
                txt_Minat.Text = .Item("minat").ToString

                If .Item("foto").ToString <> "-" Then
                    If My.Computer.FileSystem.FileExists(APP.PATH_IMG & .Item("foto").ToString) Then
                        lbl_filename.Text = .Item("foto").ToString
                        pct_gambar.Image = Image.FromFile(APP.PATH_IMG & .Item("foto").ToString)
                    End If
                End If
            End With
        End If
    End Sub

   

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        txt_NIM.Text = ""
        txt_Nama.Text = ""
        txt_Tmp_Lahir.Text = ""
        cmb_tanggal_lahir.Value = Now
        txt_Alamat.Text = ""
        txt_Telp.Text = ""
        txt_Email.Text = ""
        txt_Jurusan.Text = ""
        cmb_Tahun.Text = "- Tahun -"
        txt_Minat.Text = ""
    End Sub

    Private Sub Frm_Manajemen_Anggota_add_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txt_jurusan.Items.Add("Sistem Informasi")
        txt_jurusan.Items.Add("Teknik Informatika")
        txt_jurusan.Items.Add("Manajemen Bisnis Informatika")

        Dim i As Integer
        For i = 1993 To 2020
            cmb_Tahun.Items.Add(i)
        Next i
    End Sub

    Private Sub cmdSave_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Validation.clearRules()
        Validation.addRules(txt_NIM.Text, "NIM", "required|length[1-7]")
        Validation.addRules(txt_Nama.Text, "Nama", "required|length[1-50]")
        Validation.addRules(txt_Tmp_Lahir.Text, "Tempat Lahir", "required|length[1-30]")
        Validation.addRules(txt_Alamat.Text, "Alamat", "required|length[1-150]")
        Validation.addRules(txt_Telp.Text, "Telepon", "required|numeric")
        Validation.addRules(txt_Email.Text, "E-mail", "required|email")
        Validation.addRules(txt_Jurusan.Text, "Jurusan", "required|length[1-100]")
        Validation.addRules(txt_Minat.Text, "Minat", "required|length[1-255]")

        If Validation.isValid Then
            If changePicture = True Then
                '# operasi file image
                If lbl_filename.Text <> "-" Then
                    With OpenFileDialog1
                        If .FileName <> vbNullString Then
                            My.Computer.FileSystem.CopyFile(.FileName, APP.PATH_IMG & lbl_filename.Text, True)
                        End If
                    End With
                End If
            End If

            Db.FlushCache()
            Db.Update("tbl_anggota")
            Db.SetField("nim", txt_NIM.Text)
            Db.SetField("nama", txt_Nama.Text)
            Db.SetField("tmp_lahir", txt_Tmp_Lahir.Text)
            Db.SetField("tgl_lahir", cmb_tanggal_lahir.Value.Date)
            Db.SetField("alamat", txt_Alamat.Text)
            Db.SetField("telp", txt_Telp.Text)
            Db.SetField("email", txt_Email.Text)
            Db.SetField("jurusan", txt_Jurusan.Text)
            Db.SetField("tahun", cmb_Tahun.Text)
            Db.SetField("minat", txt_Minat.Text)
            Db.SetField("foto", lbl_filename.Text)
            Db.Where("nim", txt_NIM.Text)

            If Connection.ExecuteNonQuery(Db.GetQueryString()) Then
                Frm_Manajemen_Anggota_view.InitGrid()
                MsgBox("Data Berhasil Disimpan")
                Me.Close()
                'cmdSave_Click_1(sender, e)
            Else
                MsgBox("Gagal Menyimpan data")
            End If

        Else
            Validation.showMessage()
        End If
    End Sub

    Private Sub cmdCancel_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        End
    End Sub

    

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        pct_gambar.Image = Nothing
        lbl_filename.Text = "-"
        changePicture = True
        cmd_browse.Enabled = True
    End Sub

    Private Sub cmd_browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_browse.Click
        With OpenFileDialog1
            .Filter = "Image File (PNG, BMP, JPG, JPEG)|*.png;*.bmp;*.jpg;*.jpeg|" & _
                        "All File|*.*"

            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                Try
                    lbl_filename.Text = .SafeFileName
                    pct_gambar.Image = Image.FromFile(.FileName)
                Catch ex As Exception

                End Try
            End If

        End With
    End Sub
End Class